{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SageMaker V3 JumpStart Model Example\n",
    "\n",
    "This notebook demonstrates how to use SageMaker V3 ModelBuilder with JumpStart models for easy model deployment and inference."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prerequisites\n",
    "Note: Ensure you have sagemaker and ipywidgets installed in your environment. The ipywidgets package is required to monitor endpoint deployment progress in Jupyter notebooks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import required libraries\n",
    "import json\n",
    "import uuid\n",
    "\n",
    "from sagemaker.serve.model_builder import ModelBuilder\n",
    "from sagemaker.core.jumpstart.configs import JumpStartConfig\n",
    "from sagemaker.core.resources import EndpointConfig\n",
    "from sagemaker.train.configs import Compute"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 1: Configure JumpStart Model\n",
    "\n",
    "We'll use a HuggingFace Falcon model from JumpStart for this example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Configuration\n",
    "MODEL_ID = \"huggingface-llm-falcon-7b-bf16\"\n",
    "MODEL_NAME_PREFIX = \"js-v3-example-model\"\n",
    "ENDPOINT_NAME_PREFIX = \"js-v3-example-endpoint\"\n",
    "\n",
    "# Generate unique identifiers\n",
    "unique_id = str(uuid.uuid4())[:8]\n",
    "model_name = f\"{MODEL_NAME_PREFIX}-{unique_id}\"\n",
    "endpoint_name = f\"{ENDPOINT_NAME_PREFIX}-{unique_id}\"\n",
    "\n",
    "print(f\"Model name: {model_name}\")\n",
    "print(f\"Endpoint name: {endpoint_name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Create ModelBuilder from JumpStart Config\n",
    "\n",
    "The ModelBuilder can automatically configure itself from a JumpStart model ID."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize model_builder object with JumpStart configuration\n",
    "compute = Compute(instance_type=\"ml.g5.2xlarge\")\n",
    "jumpstart_config = JumpStartConfig(model_id=MODEL_ID)\n",
    "model_builder = ModelBuilder.from_jumpstart_config(jumpstart_config=jumpstart_config, compute=compute)\n",
    "\n",
    "print(\"ModelBuilder created successfully from JumpStart config!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 3: Build the Model\n",
    "\n",
    "Build the model artifacts and prepare for deployment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Build the model\n",
    "core_model = model_builder.build(model_name=model_name)\n",
    "print(f\"Model Successfully Created: {core_model.model_name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 4: Deploy the Model\n",
    "\n",
    "Deploy the model to a SageMaker endpoint for real-time inference."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Deploy the model to an endpoint\n",
    "core_endpoint = model_builder.deploy(endpoint_name=endpoint_name)\n",
    "print(f\"Endpoint Successfully Created: {core_endpoint.endpoint_name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 5: Test the Endpoint\n",
    "\n",
    "Send a test request to the deployed endpoint."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test the endpoint with a sample query\n",
    "test_data = {\"inputs\": \"What are falcons?\", \"parameters\": {\"max_new_tokens\": 32}}\n",
    "\n",
    "result = core_endpoint.invoke(\n",
    "    body=json.dumps(test_data),\n",
    "    content_type=\"application/json\"\n",
    ")\n",
    "\n",
    "# Decode and display the result\n",
    "prediction = json.loads(result.body.read().decode('utf-8'))\n",
    "print(f\"Model Response: {prediction}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 6: Clean Up Resources\n",
    "\n",
    "Clean up the created resources to avoid ongoing charges."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Clean up resources\n",
    "core_endpoint_config = EndpointConfig.get(endpoint_config_name=core_endpoint.endpoint_name)\n",
    "\n",
    "# Delete in the correct order\n",
    "core_model.delete()\n",
    "core_endpoint.delete()\n",
    "core_endpoint_config.delete()\n",
    "\n",
    "print(\"All resources successfully deleted!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "This notebook demonstrated:\n",
    "1. Creating a ModelBuilder from JumpStart configuration\n",
    "2. Building a model from JumpStart\n",
    "3. Deploying to a SageMaker endpoint\n",
    "4. Making inference requests\n",
    "5. Cleaning up resources\n",
    "\n",
    "The V3 ModelBuilder makes it easy to work with JumpStart models with minimal configuration required!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv-test",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
